﻿using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using RxjhServer.HelperTools;

namespace RxjhServer.bbg
{
	public class SockClienT
	{
		public Socket clientSocket;

		private byte[] dataReceive;

		private bool disposed;

		private MemoryStream ms;

		private RemoveClientDelegateE removeFromTheServerList;

		public event MessageeDelegaterE OnSockMessage;

		public bool Disposed
		{
			get
			{
				return this.disposed;
			}
		}

		public IPAddress IP
		{
			get
			{
				try
				{
					IPAddress result;
					if (this.disposed)
					{
						result = null;
						return result;
					}
					result = ((IPEndPoint)this.clientSocket.RemoteEndPoint).Address;
					return result;
				}
				catch
				{
					this.Dispose();
				}
				return null;
			}
		}

		public SockClienT(Socket from, RemoveClientDelegateE rftsl)
		{
			this.dataReceive = new byte[1500];
			this.disposed = false;
			this.ms = new MemoryStream();
			this.removeFromTheServerList = rftsl;
			this.clientSocket = from;
		}

		public void Dispose()
		{
			if (!this.disposed)
			{
				this.disposed = true;
				try
				{
					if (this.removeFromTheServerList != null)
					{
						this.removeFromTheServerList(this);
					}
					this.clientSocket.Shutdown(SocketShutdown.Both);
				}
				catch
				{
				}
				if (this.clientSocket != null)
				{
					this.clientSocket.Close();
				}
				this.clientSocket = null;
			}
		}

		public virtual void OnReceiveData(IAsyncResult ar)
		{
			try
			{
				if (!this.disposed)
				{
					int num = this.clientSocket.EndReceive(ar);
					if (num <= 0)
					{
						this.Dispose();
					}
					else
					{
						this.ProcessDataReceived(this.dataReceive, num);
						this.Dispose();
					}
				}
			}
			catch (Exception)
			{
				this.Dispose();
			}
		}

		public void OnSended(IAsyncResult ar)
		{
			try
			{
				if (!this.disposed)
				{
					this.clientSocket.EndSend(ar);
					this.clientSocket.BeginReceive((ar.AsyncState as SockClienT).dataReceive, 0, (ar.AsyncState as SockClienT).dataReceive.Length, SocketFlags.None, new AsyncCallback(this.OnReceiveData), ar.AsyncState);
				}
			}
			catch
			{
				this.Dispose();
			}
		}

		public void OnSended2(IAsyncResult ar)
		{
			try
			{
				if (!this.disposed)
				{
					this.clientSocket.EndSend(ar);
				}
			}
			catch (Exception)
			{
				this.Dispose();
			}
		}

		public virtual void ProcessDataReceived(byte[] data, int length)
		{
		}

		public virtual void Sendd(string str)
		{
			byte[] bytes = Encoding.Default.GetBytes(str);
			this.Send(bytes, bytes.Length);
		}
		public virtual void Sendd1(string str)//寒风发包
		{
			byte[] bytes = Encoding.Default.GetBytes(str);
			this.Senddd(bytes, bytes.Length);
		}

		public virtual void Senddd(byte[] toSendBuff, int len)//寒风发包
		{
			try
			{
				if (this.disposed)
					return;
				byte[] buffer = new byte[len + 6];
				buffer[0] = (byte)170;
				buffer[1] = (byte)102;
				Buffer.BlockCopy((Array)BitConverter.GetBytes(len), 0, (Array)buffer, 2, 4);
				Buffer.BlockCopy((Array)toSendBuff, 0, (Array)buffer, 6, len);
				this.clientSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(this.OnSended2), (object)this);
			}
			catch (Exception ex)
			{
				this.Dispose();
			}
		}

		public virtual void Send(string str)
		{
			try
			{
				int size = 0;
				if (!this.disposed)
				{
					byte[] array = new byte[str.Length];
					Converter.ToBytes(str, array, ref size);
					this.clientSocket.BeginSend(array, 0, size, SocketFlags.None, new AsyncCallback(this.OnSended2), this);
				}
			}
			catch (Exception)
			{
				this.Dispose();
			}
		}

		public virtual void Send(byte[] toSendBuff, int len)
		{
			try
			{
				if (!this.disposed)
				{
					byte[] array = new byte[len + 6];
					array[0] = 170;
					array[1] = 136;
					Buffer.BlockCopy(BitConverter.GetBytes(len), 0, array, 2, 4);
					Buffer.BlockCopy(toSendBuff, 0, array, 6, len);
					this.clientSocket.BeginSend(array, 0, array.Length, SocketFlags.None, new AsyncCallback(this.OnSended2), this);
				}
			}
			catch (Exception)
			{
				this.Dispose();
			}
		}

		public virtual void Send(char[] toSendBuff, int len)
		{
			try
			{
				if (!this.disposed)
				{
					byte[] array = new byte[len];
					Buffer.BlockCopy(toSendBuff, 0, array, 0, len);
					this.clientSocket.BeginSend(array, 0, len, SocketFlags.None, new AsyncCallback(this.OnSended2), this);
				}
			}
			catch (Exception)
			{
				this.Dispose();
			}
		}

		public virtual void Send(byte[] toSendBuff, int offset, int len)
		{
			try
			{
				if (!this.disposed)
				{
					byte[] array = new byte[len];
					Buffer.BlockCopy(toSendBuff, offset, array, 0, len);
					if (!this.disposed)
					{
						this.clientSocket.BeginSend(array, 0, len, SocketFlags.None, new AsyncCallback(this.OnSended2), this);
					}
				}
			}
			catch (Exception)
			{
				this.Dispose();
			}
		}

		public void Start()
		{
			this.clientSocket.BeginReceive(this.dataReceive, 0, this.dataReceive.Length, SocketFlags.None, new AsyncCallback(this.OnReceiveData), this);
		}

		public void RaiseMessageEvent(string Msg)
		{
			if (this.OnSockMessage != null)
			{
				this.OnSockMessage(Msg, this);
			}
		}
	}
}
